What is @peculiar/asn1-x509?
@peculiar/asn1-x509 is an npm package that provides tools for working with ASN.1 (Abstract Syntax Notation One) and X.509 certificates. It allows developers to parse, encode, and manipulate X.509 certificates and related structures in JavaScript.
What are @peculiar/asn1-x509's main functionalities?
Parsing X.509 Certificates
This feature allows you to parse a raw X.509 certificate from a binary format (BER). The code sample demonstrates how to convert a base64-encoded certificate into a Certificate object.
const { Certificate } = require('@peculiar/asn1-x509');
const rawCert = Buffer.from('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1J...', 'base64');
const cert = Certificate.fromBER(rawCert);
console.log(cert);
Encoding X.509 Certificates
This feature allows you to encode an X.509 certificate into a binary format (BER). The code sample demonstrates how to create a Certificate object and convert it into a base64-encoded string.
const { Certificate } = require('@peculiar/asn1-x509');
const cert = new Certificate({
tbsCertificate: {
version: 2,
serialNumber: new Uint8Array([1, 2, 3, 4]),
signature: { algorithm: '1.2.840.113549.1.1.11' },
issuer: { type: 'rdnSequence', value: [] },
validity: { notBefore: new Date(), notAfter: new Date() },
subject: { type: 'rdnSequence', value: [] },
subjectPublicKeyInfo: { algorithm: { algorithm: '1.2.840.113549.1.1.1' }, subjectPublicKey: new Uint8Array([1, 2, 3, 4]) }
},
signatureAlgorithm: { algorithm: '1.2.840.113549.1.1.11' },
signatureValue: new Uint8Array([1, 2, 3, 4])
});
const rawCert = cert.toSchema().toBER(false);
console.log(Buffer.from(rawCert).toString('base64'));
Manipulating Certificate Extensions
This feature allows you to manipulate extensions within an X.509 certificate. The code sample demonstrates how to add a new extension to a Certificate object.
const { Certificate, Extension } = require('@peculiar/asn1-x509');
const cert = new Certificate();
const ext = new Extension({
extnID: '2.5.29.14',
critical: false,
extnValue: new Uint8Array([1, 2, 3, 4])
});
cert.tbsCertificate.extensions = [ext];
console.log(cert.tbsCertificate.extensions);
Other packages similar to @peculiar/asn1-x509
node-forge
node-forge is a robust library for implementing various cryptographic functions in JavaScript, including working with X.509 certificates. It provides a higher-level API compared to @peculiar/asn1-x509 and includes additional features like PKCS#12 and TLS.
asn1js
asn1js is a library for encoding and decoding ASN.1 data structures in JavaScript. It is more general-purpose compared to @peculiar/asn1-x509, which is specifically tailored for X.509 certificates.
pkijs
pkijs is a library built on top of asn1js for working with PKI (Public Key Infrastructure) in JavaScript. It provides comprehensive support for X.509 certificates, CRLs, OCSP, and more, making it a more feature-rich alternative to @peculiar/asn1-x509.